package org.jeecg.modules.simulator.rooter;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.simulator.factory.*;
import org.jeecg.modules.simulator.jsonot.NumPeopEnStationApiInfo;
import org.jeecg.modules.simulator.jsonot.ODProbabilityApiInfo;
import org.jeecg.modules.simulator.jsonot.PsgStationDistributionInfo;
import org.jeecg.modules.simulator.jsonot.RouteStation;
import org.jeecg.modules.simulator.model.SceneDataType;
import org.jeecg.modules.simulator.model.line.park.Parker;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.person.Person;
import org.jeecg.modules.simulator.model.train.timeplan.TrainPlanTable;
import org.jeecg.modules.simulator.service.HistoryBizPersonLoaderService;
import org.jeecg.modules.simulator.service.HistoryBizTrainLoaderService;

import org.jeecg.modules.simulator.util.UnitCache;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-24 13:28
 **/
@Slf4j
public class RooterBizLoader {
    private static final Logger logger = LoggerFactory.getLogger(RooterBizLoader.class);

    @Setter
    @Getter
    String sceneCode;
    @Setter
    @Getter
    DataBizFactory dataBizLoader ;

//    @Getter
//    @Setter
//    T80RouteHisService t80RouteHisService;
    @Getter
    @Setter
    //按线路分配的场段字典
    Map<String, List<Parker>> parkerMap = new HashMap<>();
    @Getter
    @Setter
    //按id为key做的场段字典，给trainListener使用的
    Map<String, Parker> parkersMap = new HashMap<>();
    @Getter
    @Setter
    //总station集合，使用的是stationId,做key
    Map<String, Station> stationMap = new HashMap<>();
    @Getter
    @Setter
    //换乘站集合
    Map<String, TransferStation> transferStationMap = new HashMap<>();
    @Getter
    @Setter
    HistoryBizTrainLoaderService historyBizTrainLoaderService;
    @Getter
    @Setter
    HistoryBizPersonLoaderService historyBizPersonLoaderService;



    @Getter
    Map<String, List<TrainPlanTable>> trainPlanTableMapByLine = new HashMap<>();
    @Getter
    Map<String, Map<String, List<Person>>> personTrainMapByLine = new HashMap<>();


//    @Getter
//    Map<String, List<ODProbabilityApiInfo>> odProbabilityApiInfoMap = new HashMap<>();
//    @Getter
//    Map<String, Map<String, NumPeopEnStationApiInfo>> numPeopEnStationApiInfoMap = new HashMap<>();
//    @Getter
//    Map<String, List<PsgStationDistributionInfo>> psgStationDistributionInfoMap = new HashMap<>();
//    @Getter
//    Map<String, List<RouteStation>> t80RouteHisMap = new HashMap<>();



    //region 加载业务数据
    /**
     * @description: 加载业务数据，包括列车时刻表、OD概率数据和站点数据。
     *
     * @author:  renzhg
     * @date:  2024/11/29 10:50
     **/
    public void loadBizData() {
        SceneDataType sceneDataType = UnitCache.sceneParamMap.get(sceneCode).getSceneType();
        dataBizLoader.setSceneCode(sceneCode);
        //设置换乘站和站点信息用于查询使用
        setDataBizStationMapParams();
        //数据库查询人员路径表
//        loadT80Route(); // Load personnel path table

        if (sceneDataType.equals(SceneDataType.allDayHistoryData)) {

            loadTranData();
           /**
            * @description:
            * 1、 加载列车相关数据包括列车类型数据和列车时刻表数据
            * 2、加载OD概率数据
            * 3、加载进站人数数据
            *
            * @author:  renzhg
            * @date:  2024/11/29 10:43
            **/

//            loadAllDayHistoryData();





        } else if(sceneDataType.equals(SceneDataType.customJson)){
            loadCustomJsonData();
        }
//        else if(sceneDataType.equals(SceneDataType.timePeriodHistoryData)){
//            loadTimePeriodHistoryData();
//        }
        //把查询完成的列车时刻表数据放在这个相应的对象中
        loadTrainPlanTableParam();
//        设置停车场段站点对应的出站站点数据
        updateStationData();

    }

    private void loadTranData() {
        LoadHistoryTrainData loadTrainPeriodHistory=new LoadAllDayTrainHistory(historyBizTrainLoaderService);
        dataBizLoader.loadTrainBizData(loadTrainPeriodHistory);



//        dataBizLoader.loadTrain(loadTimePeriodHistory);//列车时刻表加载

//        dataBizLoader.loadBiz(loadAllDayHistory);
//        加载列车相关数据包括列车类型数据和列车时刻表数据


    }

    private void loadTrainPlanTableParam() {
        trainPlanTableMapByLine=dataBizLoader.getTrainPlanTableMapByLine();
//        personTrainMapByLine=dataBizLoader.getPersonTrainMapByLine();
    }

    private void setDataBizStationMapParams() {
        dataBizLoader.setTransferStationMap(transferStationMap);
        dataBizLoader.setStationMap(stationMap);
    }

    private void loadCustomJsonData() {

        DataCustomJsonBizFactory dataCustomJsonBizFactory = new DataCustomJsonBizFactory();
        dataCustomJsonBizFactory.loadCustomDataBiz();
//        odProbabilityApiInfoMap = dataCustomJsonBizFactory.getOdProbabilityApiInfoMap();
//        psgStationDistributionInfoMap = dataBizLoader.getPsgStationDistributionInfoMap();
    }

    /**
     * @description:
     * 1、 加载列车相关数据包括列车类型数据和列车时刻表数据
     * 2、加载OD概率数据---不要了
     *3、加载进站人数数据----不要了
     * 这里只有加载车的数据，把加载人的数据分离出去
     *
     *
     * @author:  renzhg 
     * @date:  2024/11/29 10:43
     **/
    private void loadAllDayHistoryData() {




        /**
         * @description:这里因为分离出加载人的数据，所以下面的代码注释掉了
         *
         * @author:  renzhg
         * @date:  2024/11/29 10:43
         **/
//        //加载OD概率数据
//        odProbabilityApiInfoMap = loadAllDayHistory.getOdProbabilityApiInfoMap();
//        //加载进站人数数据
//        numPeopEnStationApiInfoMap = loadAllDayHistory.getNumPeopEnStationApiInfoMap();
    }

    private void loadTimePeriodHistoryData() {
//        LoadTimePeriodHistory loadTimePeriodHistory = new LoadTimePeriodHistory();
//        loadTimePeriodHistory.setHistoryBizTrainLoaderService(historyBizTrainLoaderService);
//        loadTimePeriodHistory.setHistoryBizPersonLoaderService(historyBizPersonLoaderService);
//        loadTimePeriodHistory.setT80RouteHisMap(t80RouteHisMap);
//        dataBizLoader.loadBiz(loadTimePeriodHistory);
////        odProbabilityApiInfoMap = loadTimePeriodHistory.getOdProbabilityApiInfoMap();
////        numPeopEnStationApiInfoMap= loadTimePeriodHistory.getNumPeopEnStationApiInfoMap();
////        psgStationDistributionInfoMap = dataBizLoader.getPsgStationDistributionInfoMap();
    }
// 设置停车场段站点对应的出站站点数据
    private void updateStationData() {
//       设置停车场段站点对应的出站站点数据
        updateParkerMaps();

//        updateParkersMap();
    }


    /**
     * @description: 设置停车场段站点对应的出站站点数据
     *
     * @author:  renzhg
     * @date:  2024/11/29 10:26
     **/
    private void updateParkerMaps() {
        parkersMap.forEach((k, v) -> {
            if(stationMap.containsKey(v.getUpOutStationId())){
                v.setUpOut(stationMap.get(v.getUpOutStationId()));
            }
            if(stationMap.containsKey(v.getDownOutStationId())){
                v.setDownOut(stationMap.get(v.getDownOutStationId()));
            }
        });
    }
/**
 * @description: 设置停车场轨道数据
 *
 * @author:  renzhg
 * @date:  2024/11/29 10:27
 **/
    private void updateParkersMap(){
        parkerMap.forEach((k, v) -> {
        v.forEach(o -> {
            if (stationMap.containsKey(o.getUpOutStationId())) {
                o.setUpOut(stationMap.get(o.getUpOutStationId()));
            }
            if (stationMap.containsKey(o.getDownOutStationId())) {
                o.setDownOut(stationMap.get(o.getDownOutStationId()));
            }
        });
        });
    }

//    public void loadT80Route() {
//        t80RouteHisService.load();
//        // 1、人员路径表
//        t80RouteHisMap = t80RouteHisService.getT80RouteHisMap();
//        logger.debug("wanch");
//
//    }
    //endregion
}
